package com.cuanbo.dualscreenteachfu.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

import androidx.appcompat.widget.AppCompatImageView;

import com.cuanbo.dualscreenteachfu.R;
import com.cuanbo.dualscreenteachfu.utils.CmdUtil;
import com.cuanbo.dualscreenteachfu.utils.SharedPreferencesUtil;
import com.cuanbo.dualscreenteachfu.utils.StringUtil;

/**
 * 带指令功能的开关控件
 */
public class CmdSwitchView extends AppCompatImageView implements View.OnClickListener {
    /**
     * 选中时的图片id
     */
    private Drawable checkDrawable;
    /**
     * 未选中时的图片id
     */
    private Drawable unCheckDrawable;

    private boolean check;

    public CmdSwitchView(Context context) {
        this(context, null);
    }

    public CmdSwitchView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CmdSwitchView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.cmdview, defStyleAttr, 0);
        checkDrawable = a.getDrawable(R.styleable.cmdview_checkImg);
        unCheckDrawable = a.getDrawable(R.styleable.cmdview_unCheckImg);
        init();
    }

    private void init() {
        setOnClickListener(this);
        setImageDrawable(unCheckDrawable);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        String tag = (String) getTag();
        if (StringUtil.checkIpFormat(tag) || tag.equals("华为屏总")) {
            check = SharedPreferencesUtil.getBoolean(getContext(), tag, false);
            if (check) {
                setImageDrawable(checkDrawable);
            } else {
                setImageDrawable(unCheckDrawable);
            }
        } else {
            //读取本地保存的设备状态并更新UI
            String cmd = SharedPreferencesUtil.getString(getContext(), tag);
            //LogUtil.add("init", "读取本地保存cmd=" + cmd, "CmdSwitchView");
            if (cmd != null) {
                changeUI(cmd);
            }
        }
    }

    /**
     * 设置状态
     *
     * @param check True:选中 False:未选中
     */
    private CmdSwitchView setCheck(boolean check) {
        String cmd = (String) getTag();
        CmdUtil.add(cmd, check);
        return this;
    }

    /**
     * 根据回传过来的指令改变开关状态
     *
     * @param cmd 指令
     */
    public CmdSwitchView changeUI(String cmd) {
        String cmdTag = (String) getTag();
        if ((cmdTag + "开").equals(cmd)) {
            check = true;
            setImageDrawable(checkDrawable);
        } else if ((cmdTag + "关").equals(cmd)) {
            check = false;
            setImageDrawable(unCheckDrawable);
        }

        return this;
    }

    /**
     * 取相反状态
     */
    public CmdSwitchView adverse() {
        setCheck(!check);
        return this;
    }

    @Override
    public void onClick(View v) {
        adverse();
    }

}
